/* 
 *  <copyright file="OnCollisionBase.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using System.Text;

using SobrietyEngine.Common;
using SobrietyEngine.Components.Physics;
using FarseerPhysics;
using FarseerPhysics.Collision;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;

namespace SobrietyEngine.Components.Physics.OnCollision
{
    public abstract class OnCollisionBase:BaseComponent, SobrietyEngine.Components.IComponent
    {
        protected Boolean enableCollisionResponse = true;
        public Boolean EnableCollisionResponse 
        { 
            get { return enableCollisionResponse; }
            set
            {
                if (enableCollisionResponse == false && value == true)
                    physCmp.Body.OnCollision += this.OnCollision;
                else if (enableCollisionResponse == true && value == false)
                    physCmp.Body.OnCollision -= this.OnCollision;
                enableCollisionResponse = value;                
            }
        }

        protected Boolean enableSeperationResponse;
        public Boolean EnableSeperationResponse 
        {
            get { return enableSeperationResponse; }
            set
            {
                if (EnableSeperationResponse == false && value == true)
                    physCmp.Body.OnSeparation += this.OnSeperation;
                else if (enableSeperationResponse == true && value == false)
                    physCmp.Body.OnSeparation -= this.OnSeperation;
                enableSeperationResponse = value;
            }
        }

        IFarseerPhysicsBody physCmp = null;

        public OnCollisionBase()
            : base()
        {
        }

        public override List<ParameterDescription> GetParameterDescriptions()
        {
            List<ParameterDescription> d = new List<ParameterDescription>();
            d.Add(new ParameterDescription("EnableCollisionResponse", "Is collision handler enabled", typeof(Boolean), true));
            d.Add(new ParameterDescription("EnableSeperationResponse", "Is seperation handler enabled", typeof(Boolean), false));
            return d;
        }

        override public void Set(Parameters parms)
        {
            EnableCollisionResponse = parms.GetValueAs<Boolean>("EnableCollisionResponse", true);
            EnableSeperationResponse = parms.GetValueAs<Boolean>("EnableSeperationResponse", true);
        }

        public override Boolean InitComponent()
        {
            base.InitComponent();
            physCmp = Actor.Get<IFarseerPhysicsBody>();
            if (physCmp == null)
                throw new Exception("physics body is null!");

            if (physCmp.Body == null)
                return false; //our physics component has not yet been initialized, delay this init

            if(EnableCollisionResponse)
                physCmp.Body.OnCollision += this.OnCollision;            
            if(EnableSeperationResponse)
                physCmp.Body.OnSeparation += this.OnSeperation;

            return true;
        }

        virtual public bool OnCollision(Fixture me, Fixture other, Contact contact)
        {
            return true;
        }

        virtual public void OnSeperation(Fixture me, Fixture other)
        {
        }
    }
}